Cheap (But Functional) Threads
نویسندگان
چکیده
This article demonstrates how a powerful and expressive abstraction from concurrency theory plays a dual rôle as a programming tool for concurrent applications and as a foundation for their verification. This abstraction—monads of resumptions expressed using monad transformers—is cheap: it is easy to understand, easy to implement, and easy to reason about. We illustrate the expressiveness of the resumption monad with the construction of an exemplary multitasking operating system kernel with process forking, preemption, message passing, and synchronization constructs in the pure functional programming language Haskell. Because resumption computations are stream-like structures, reasoning about this kernel may be posed as reasoning about streams, a problem which is well-understood. And, as an example, this article illustrates how a liveness property— fairness—is proven and especially how the resumption-monadic structure promotes such verifications. 1 “Cheap” Concurrency This paper presents a low overhead approach to multi-threaded concurrent computation. We demonstrate how a rich variety of concurrent behaviors—including synchronization, message passing, the forking and suspension of threads among others—may be expressed succinctly in a non-strict functional language with no changes to the host language implementation. The necessary machinery—monads of resumptions—is so expressive, in fact, that the kernel described here requires fewer than fifty lines of Haskell 98 code. And, because this machinery may be generalized as monad transformers, the functionality described here may be reused and refined easily. Many techniques and structures have emigrated from programming language theory to programming practice (e.g., types, CPS, etc.), and this article advocates that resumption monads make the journey as well. This work is not intended to be theoretical; on the contrary, its purpose is to demonstrate how a natural (but, perhaps, under-appreciated) computational model of concurrency is used to construct and verify concurrent applications. The approach taken here is informal and should be accessible to anyone familiar with monads in Haskell. And while the constructions 1 All the code presented in this paper is available from the author.
منابع مشابه
QTk: An Integrated Model-Based Approach to Designing Executable User Interfaces
QTk is a tool built on top of Tcl/Tk that allows user interface designers to adopt a cost-effective model-based approach for designing executable user interfaces. QTk is based on a descriptive approach that uses a declarative style where appropriate (symbolic records to specify widget types, initial states, and geometry management; procedure values to specify actions), augmented with objects an...
متن کاملCPC: programming with a massive number of lightweight threads
Threads are a convenient and modular abstraction for writing concurrent programs. Unfortunately, threads, as they are usually implemented, are fairly expensive, which often forces the programmer to use a somewhat coarser concurrency structure than he would want to. The standard alternative to threads, event-loop programming, allows much lighter units of concurrency; however, event-loop programm...
متن کاملThreads on the Cheap: Multithreaded Execution in a WaveCache Processor
Executing multiple threads on a single processor will play a key role the future scaling of computer performance, and while many new architectures propose novel uses for threads, few address the complexity required to support multiple threads in a single processor core. This paper describes extensions to WaveScalar, a recently proposed dataflow instruction set, and the WaveCache, a WaveScalar p...
متن کاملDynamic Cheap Eagerness
Dynamic cheap eagerness extends cheap eagerness by allowing the decision of whether to build a thunk or speculatively evaluate its body to be deferred until run time. We have implemented this optimi-sation in a compiler for a simple functional language and measured its eeect on a few benchmarks. It turns out that a large part of the overhead of graph reduction can be eliminated, but that run-ti...
متن کاملThe Random Traversal Technique for Parallel Evaluation of Functional Programs
Many functional programs contain significant amounts of natural parallelism, but taking advantage of this parallelism in a scalable way is difficult. Previous work on parallel evaluation of functional programs has focused on spawning evaluations in other threads. The threads must synchronize to notify other threads of work and perform load balancing. This synchronization is generally slow so th...
متن کامل